package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    //检索控制器 接收到用户检索的参数
    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model){
        Result<Map> result = listFeignClient.search(searchParam);
        model.addAllAttributes(result.getData());
        //url
        String urlParam = this.makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);

        //制作品牌面包屑  格式：   品牌：品牌名称
        String trademarkParam = this.makeTradeMarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam",trademarkParam);

        //平台属性面包屑  格式：  平台属性名：平台属性值名称
        //List<Map> propsParamList = this.makePropsParamList(searchParam.getProps());
        List<SearchAttr> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("propsParamList",propsParamList);

        //制作排序
        Map<String,Object> orderMap = this.makeOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);

        model.addAttribute("searchParam",searchParam);
        return "list/index";
    }

    //制作排序
    private Map<String, Object> makeOrderMap(String order) {
        //order = 2:asc order=2:desc

        Map<String, Object> map = new HashMap<>();
        if(!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if(split!=null && split.length>0){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    //平台属性面包屑
    /*private List<Map> makePropsParamList(String[] props) {
        List<Map> list = new ArrayList<>();
        if(props!=null && props.length>0){
            //prop = 24:256G: 机身内存
            for (String prop : props) {
                String[] split = prop.split(":");
                if(split!=null && split.length>0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }*/
    private List<SearchAttr> makePropsParamList(String[] props) {
        List<SearchAttr> list = new ArrayList<>();
        if(props!=null && props.length>0){
            //prop = 24:256G: 机身内存
            for (String prop : props) {
                String[] split = prop.split(":");
                if(split!=null && split.length>0){
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    list.add(searchAttr);
                }
            }
        }
        return list;
    }

    //制作品牌面包屑
    private String makeTradeMarkParam(String trademark) {
        //判断条件不为空
        //trademark数据格式  trademark=1：小米
        if(!StringUtils.isEmpty(trademark)){
            //分割字符串
            String[] split = trademark.split(":");
            if(split!=null && split.length>0){
                return "品牌："+split[1];
            }
        }
        return null;
    }

    //制作url拼接
    private String makeUrlParam(SearchParam searchParam) {

        StringBuilder stringBuilder = new StringBuilder();
        //判断用户是否通过分类id进行检索
        if(!StringUtils.isEmpty(searchParam.getCategory3Id())){
            stringBuilder.append("getCategory3Id=").append(searchParam.getCategory3Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory2Id())){
            stringBuilder.append("getCategory2Id=").append(searchParam.getCategory2Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory1Id())){
            stringBuilder.append("getCategory1Id=").append(searchParam.getCategory1Id());
        }

        //判断是否通过关键词检索
        if(!StringUtils.isEmpty(searchParam.getKeyword())){
            stringBuilder.append("keyword=").append(searchParam.getKeyword());
        }

        //通过品牌过滤
        if(!StringUtils.isEmpty(searchParam.getTrademark())){
            if(stringBuilder.length()>0){
                stringBuilder.append("&trademark=").append(searchParam.getTrademark());
            }
        }

        //平台属性
        String[] props = searchParam.getProps();
        if(props!=null && props.length>0){
            for (String prop : props) {
                if(stringBuilder.length()>0){
                    stringBuilder.append("&props=").append(prop);
                }
            }
        }
        return "list.html?"+stringBuilder.toString();
    }
}
